package com.dyh.quickdemo.stub;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

/*
@Author=Joey Wheeler
@Date=2021/4/15 14:05
*/
@Slf4j
public class StubThreadLocalSupportTest {

    @Test
    public void test2() throws InterruptedException {
        Thread t1 = new Thread(
                this::f2
        );

        Thread t2 = new Thread(
                this::f2
        );

        t1.start();
        t2.start();

        t1.join();
        t2.join();
    }

    public void f2(){
        Stub stub =new Stub(11L);
        StubThreadLocalSupport.set(stub);
        try {
            f22();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            log.info(JSON.toJSONString(StubThreadLocalSupport.get()));
        }
    }
    public void f22(){

        f222();
    }

    public void f222(){
        StubThreadLocalSupport.get().log(Thread.currentThread().getName());
        if (1==1)
            throw new RuntimeException();
    }




    @Test
    public void test() throws InterruptedException {

        new Thread(
                ()->{
                    try {
                        StubThreadLocalSupport.set(new Stub("1001"));
                        System.out.println(
                                Thread.currentThread().getName()+
                                JSON.toJSONString(  StubThreadLocalSupport.get())
                        );
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        //StubThreadLocalSupport.exit();
                    }
                }
        ).start();

        new Thread(
                ()->{
                    try {
                        StubThreadLocalSupport.set(new Stub("1002"));
                        System.out.println(
                                Thread.currentThread().getName()+
                                        JSON.toJSONString(  StubThreadLocalSupport.get())
                        );
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                       // StubThreadLocalSupport.exit();
                    }
                }
        ).start();

        Thread.sleep(1000L);
        System.out.println(
                Thread.currentThread().getName()+
                        JSON.toJSONString(  StubThreadLocalSupport.get())
        );
    }
}
